# -*- coding: utf-8 -*-
from datetime import timedelta
from time import time

from utils.operators.rich_sql_sensor import RichSqlSensor

from jms.dm.dm_cn_breakoff_detail_taking_dt import jms_dm__dm_cn_breakoff_detail_taking_dt

label = f"dwd_breakoff_detail_taking_{int(time())}"
timeout = timedelta(minutes=20).seconds
doris_jms_dwd__dwd_breakoff_detail_taking = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_breakoff_detail_taking',
    email=['zhangqinglin@jtexpress.com','yl_bigdata@yl-scm.com'],
    pool='broker_load_pool',
    conn_id='doris',
    pre_sql=f"""
             TRUNCATE TABLE jms_dwd.dwd_breakoff_detail_taking PARTITION (p{{{{ execution_date | date_add(-9) | cst_ds_nodash }}}},
p{{{{ execution_date | date_add(-8) | cst_ds_nodash }}}},
p{{{{ execution_date | date_add(-7) | cst_ds_nodash }}}},
p{{{{ execution_date | date_add(-6) | cst_ds_nodash }}}},
p{{{{ execution_date | date_add(-5) | cst_ds_nodash }}}},
p{{{{ execution_date | date_add(-4) | cst_ds_nodash }}}},
p{{{{ execution_date | date_add(-3) | cst_ds_nodash }}}},
p{{{{ execution_date | date_add(-2) | cst_ds_nodash }}}},
p{{{{ execution_date | date_add(-1) | cst_ds_nodash }}}});
             LOAD LABEL jms_dwd.{label} (
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_breakoff_detail_taking_dt/dt={{{{ execution_date | date_add(-9) | cst_ds }}}}/*")  
                 INTO TABLE dwd_breakoff_detail_taking FORMAT AS 'PARQUET'
                 (billcode,taking_time,read_time,network_code,network_name,network_type,agent_code,agent_name,franchisee_code,franchisee_name,order_source_code,order_source_name,breakoff_type,arrival_count,send_count,send_shipment,actual_departure_time,planned_departure_time,next_actual_arrival_time,next_planned_arrival_time,actual_trans_min,planned_trans_min,delay_trans_min,stop_min,stop_over_12,customer_code,customer_name,breakoff_scan_type,breakoff_scan_time,next_scan_code,next_scan_name,next_scan_type,next_agent_code,next_agent_name,next_franchisee_code,next_franchisee_name,next_est_scantype,next_est_scantime,end_code,end_name,breakoff_min,mage_region_code,mage_region_name)
                 SET (dt = DATE('{{{{ execution_date | date_add(-9) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_breakoff_detail_taking_dt/dt={{{{ execution_date | date_add(-8) | cst_ds }}}}/*")  
                 INTO TABLE dwd_breakoff_detail_taking FORMAT AS 'PARQUET'
                 (billcode,taking_time,read_time,network_code,network_name,network_type,agent_code,agent_name,franchisee_code,franchisee_name,order_source_code,order_source_name,breakoff_type,arrival_count,send_count,send_shipment,actual_departure_time,planned_departure_time,next_actual_arrival_time,next_planned_arrival_time,actual_trans_min,planned_trans_min,delay_trans_min,stop_min,stop_over_12,customer_code,customer_name,breakoff_scan_type,breakoff_scan_time,next_scan_code,next_scan_name,next_scan_type,next_agent_code,next_agent_name,next_franchisee_code,next_franchisee_name,next_est_scantype,next_est_scantime,end_code,end_name,breakoff_min,mage_region_code,mage_region_name)
                 SET (dt = DATE('{{{{ execution_date | date_add(-8) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_breakoff_detail_taking_dt/dt={{{{ execution_date | date_add(-7) | cst_ds }}}}/*")  
                 INTO TABLE dwd_breakoff_detail_taking FORMAT AS 'PARQUET'
                 (billcode,taking_time,read_time,network_code,network_name,network_type,agent_code,agent_name,franchisee_code,franchisee_name,order_source_code,order_source_name,breakoff_type,arrival_count,send_count,send_shipment,actual_departure_time,planned_departure_time,next_actual_arrival_time,next_planned_arrival_time,actual_trans_min,planned_trans_min,delay_trans_min,stop_min,stop_over_12,customer_code,customer_name,breakoff_scan_type,breakoff_scan_time,next_scan_code,next_scan_name,next_scan_type,next_agent_code,next_agent_name,next_franchisee_code,next_franchisee_name,next_est_scantype,next_est_scantime,end_code,end_name,breakoff_min,mage_region_code,mage_region_name)
                 SET (dt = DATE('{{{{ execution_date | date_add(-7) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_breakoff_detail_taking_dt/dt={{{{ execution_date | date_add(-6) | cst_ds }}}}/*")  
                 INTO TABLE dwd_breakoff_detail_taking FORMAT AS 'PARQUET'
                 (billcode,taking_time,read_time,network_code,network_name,network_type,agent_code,agent_name,franchisee_code,franchisee_name,order_source_code,order_source_name,breakoff_type,arrival_count,send_count,send_shipment,actual_departure_time,planned_departure_time,next_actual_arrival_time,next_planned_arrival_time,actual_trans_min,planned_trans_min,delay_trans_min,stop_min,stop_over_12,customer_code,customer_name,breakoff_scan_type,breakoff_scan_time,next_scan_code,next_scan_name,next_scan_type,next_agent_code,next_agent_name,next_franchisee_code,next_franchisee_name,next_est_scantype,next_est_scantime,end_code,end_name,breakoff_min,mage_region_code,mage_region_name)
                 SET (dt = DATE('{{{{ execution_date | date_add(-6) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_breakoff_detail_taking_dt/dt={{{{ execution_date | date_add(-5) | cst_ds }}}}/*")  
                 INTO TABLE dwd_breakoff_detail_taking FORMAT AS 'PARQUET'
                 (billcode,taking_time,read_time,network_code,network_name,network_type,agent_code,agent_name,franchisee_code,franchisee_name,order_source_code,order_source_name,breakoff_type,arrival_count,send_count,send_shipment,actual_departure_time,planned_departure_time,next_actual_arrival_time,next_planned_arrival_time,actual_trans_min,planned_trans_min,delay_trans_min,stop_min,stop_over_12,customer_code,customer_name,breakoff_scan_type,breakoff_scan_time,next_scan_code,next_scan_name,next_scan_type,next_agent_code,next_agent_name,next_franchisee_code,next_franchisee_name,next_est_scantype,next_est_scantime,end_code,end_name,breakoff_min,mage_region_code,mage_region_name)
                 SET (dt = DATE('{{{{ execution_date | date_add(-5) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_breakoff_detail_taking_dt/dt={{{{ execution_date | date_add(-4) | cst_ds }}}}/*")  
                 INTO TABLE dwd_breakoff_detail_taking FORMAT AS 'PARQUET'
                 (billcode,taking_time,read_time,network_code,network_name,network_type,agent_code,agent_name,franchisee_code,franchisee_name,order_source_code,order_source_name,breakoff_type,arrival_count,send_count,send_shipment,actual_departure_time,planned_departure_time,next_actual_arrival_time,next_planned_arrival_time,actual_trans_min,planned_trans_min,delay_trans_min,stop_min,stop_over_12,customer_code,customer_name,breakoff_scan_type,breakoff_scan_time,next_scan_code,next_scan_name,next_scan_type,next_agent_code,next_agent_name,next_franchisee_code,next_franchisee_name,next_est_scantype,next_est_scantime,end_code,end_name,breakoff_min,mage_region_code,mage_region_name)
                 SET (dt = DATE('{{{{ execution_date | date_add(-4) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_breakoff_detail_taking_dt/dt={{{{ execution_date | date_add(-3) | cst_ds }}}}/*")  
                 INTO TABLE dwd_breakoff_detail_taking FORMAT AS 'PARQUET'
                 (billcode,taking_time,read_time,network_code,network_name,network_type,agent_code,agent_name,franchisee_code,franchisee_name,order_source_code,order_source_name,breakoff_type,arrival_count,send_count,send_shipment,actual_departure_time,planned_departure_time,next_actual_arrival_time,next_planned_arrival_time,actual_trans_min,planned_trans_min,delay_trans_min,stop_min,stop_over_12,customer_code,customer_name,breakoff_scan_type,breakoff_scan_time,next_scan_code,next_scan_name,next_scan_type,next_agent_code,next_agent_name,next_franchisee_code,next_franchisee_name,next_est_scantype,next_est_scantime,end_code,end_name,breakoff_min,mage_region_code,mage_region_name)
                 SET (dt = DATE('{{{{ execution_date | date_add(-3) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_breakoff_detail_taking_dt/dt={{{{ execution_date | date_add(-2) | cst_ds }}}}/*")  
                 INTO TABLE dwd_breakoff_detail_taking FORMAT AS 'PARQUET'
                 (billcode,taking_time,read_time,network_code,network_name,network_type,agent_code,agent_name,franchisee_code,franchisee_name,order_source_code,order_source_name,breakoff_type,arrival_count,send_count,send_shipment,actual_departure_time,planned_departure_time,next_actual_arrival_time,next_planned_arrival_time,actual_trans_min,planned_trans_min,delay_trans_min,stop_min,stop_over_12,customer_code,customer_name,breakoff_scan_type,breakoff_scan_time,next_scan_code,next_scan_name,next_scan_type,next_agent_code,next_agent_name,next_franchisee_code,next_franchisee_name,next_est_scantype,next_est_scantime,end_code,end_name,breakoff_min,mage_region_code,mage_region_name)
                 SET (dt = DATE('{{{{ execution_date | date_add(-2) | cst_ds }}}}')),
                 DATA INFILE("hdfs://yl-bg-hdp/dw/hive/jms_dm.db/external/dm_cn_breakoff_detail_taking_dt/dt={{{{ execution_date | date_add(-1) | cst_ds }}}}/*")  
                 INTO TABLE dwd_breakoff_detail_taking FORMAT AS 'PARQUET'
                 (billcode,taking_time,read_time,network_code,network_name,network_type,agent_code,agent_name,franchisee_code,franchisee_name,order_source_code,order_source_name,breakoff_type,arrival_count,send_count,send_shipment,actual_departure_time,planned_departure_time,next_actual_arrival_time,next_planned_arrival_time,actual_trans_min,planned_trans_min,delay_trans_min,stop_min,stop_over_12,customer_code,customer_name,breakoff_scan_type,breakoff_scan_time,next_scan_code,next_scan_name,next_scan_type,next_agent_code,next_agent_name,next_franchisee_code,next_franchisee_name,next_est_scantype,next_est_scantime,end_code,end_name,breakoff_min,mage_region_code,mage_region_name)
                 SET (dt = DATE('{{{{ execution_date | date_add(-1) | cst_ds }}}}'))
              )
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE label = '{label}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

doris_jms_dwd__dwd_breakoff_detail_taking << jms_dm__dm_cn_breakoff_detail_taking_dt
